package com.webdesign.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webdesign.dao.mapper.TbAdminMapper;
import com.webdesign.dao.mapper.TbDisplyMapper;
import com.webdesign.dao.mapper.TbUserMapper;
import com.webdesign.dao.mapper.TbUserhomeworkMapper;
import com.webdesign.dto.DisplayDTO;
import com.webdesign.entity.TbAdmin;
import com.webdesign.entity.TbDisply;
import com.webdesign.entity.TbDisplyExample;
import com.webdesign.entity.TbUser;
import com.webdesign.entity.TbUserhomework;
import com.webdesign.service.ITbAdminService;


@Service("TbAdminService")
public class TbAdminService implements ITbAdminService {

	@Autowired
	private TbAdminMapper tbAdminMapper;
	
	@Autowired
	private TbDisplyMapper tbDisplyMapper;
	
	@Autowired
	private TbUserMapper tbUserMapper;
	
	@Autowired
	private TbUserhomeworkMapper tbUserhomeworkMapper;
	
	
	/*
	 * 用户登录
	 */
	@Override
	public TbAdmin login(String username, String password) {
		TbAdmin tbAdmin = tbAdminMapper.selectByPrimaryKey(username);
		if(tbAdmin != null){
			if(tbAdmin.getPassword().equals(password)){
				return tbAdmin;
			}else{
				TbAdmin admin = null;
				return admin;
			}
		}else{
			TbAdmin admin = null;
			return admin;
		}
	}
	
	/*
	 * 修改用户密码，先判断是否存在改用户，再判断原密码是否正确，否则返回空的对象，反之返回修改后的用户的信息
	 */
	@Override
	public TbAdmin rewrite(String username, String password, String newpwd) {
		TbAdmin tbAdmin = tbAdminMapper.selectByPrimaryKey(username);
		if(tbAdmin != null){
			if(tbAdmin.getPassword().equals(password)){
				TbAdmin admin = new TbAdmin();
				admin.setUsername(username);
				admin.setPassword(newpwd);
				admin.setQuestion(tbAdmin.getQuestion());
				admin.setAnswer(tbAdmin.getAnswer());
				tbAdminMapper.updateByPrimaryKey(admin);
				return admin;
			}else{
				TbAdmin admin = null;
				return admin;
			}
		}else{
			TbAdmin admin = null;
			return admin;
		}
	}
	
	/* 唐   5.25
	 * 显示所有优秀作业
	 */
	@Override
	public List<DisplayDTO> displyShow(){
		
		List<DisplayDTO> tbDisplyList=new ArrayList<DisplayDTO>();
		TbDisplyExample tbDisplyExample =new TbDisplyExample();
		tbDisplyExample.createCriteria().getAllCriteria();
		List<TbDisply> tbDisplayList=tbDisplyMapper.selectByExample(tbDisplyExample);
		
		for(TbDisply tbDisplay : tbDisplayList){
			TbUser tbUser = new TbUser();
			tbUser = tbUserMapper.selectByPrimaryKey(tbDisplay.getUserid());
			TbUserhomework tbUserhomework = new TbUserhomework();
			tbUserhomework = tbUserhomeworkMapper.selectByPrimaryKey(tbDisplay.getUserhomeworkid());
			String address = tbUserhomework.getAddress();
			String userhomeworkid = tbUserhomework.getUserhomeworkid();
			String hid[] = userhomeworkid.split("_");
			String imgname = tbUser.getUsername() + "_作业" + hid[1];		
			//String imgname = userhomeworkid;
			Byte flag = tbDisplay.getClassify();
			
			if(flag==1)
			{
				DisplayDTO goodhomework=new DisplayDTO();
				goodhomework.setUserhomeworkid(userhomeworkid);
				goodhomework.setAddress(address);
				goodhomework.setClassify(flag);
				goodhomework.setImgname(imgname);
				goodhomework.setImgaddr(tbDisplay.getImgaddr());
				tbDisplyList.add(goodhomework);
			}		
		}
		return tbDisplyList;
	}
	
	/*
	 * 唐   5.25
	 */
	@Override
	public Boolean setDisplayToHP(String[] userhomeworkids) {	
		int flag=0;
		String s=null;
		for(int i=0;i<userhomeworkids.length;i++)
		{
			TbDisply display = tbDisplyMapper.selectByPrimaryKey(userhomeworkids[i]);
			TbDisply tbDisplay = new TbDisply();
			Byte classify = 2;
			tbDisplay.setClassify(classify);
			tbDisplay.setComment(display.getComment());
			tbDisplay.setCreatetime(display.getCreatetime());
			tbDisplay.setImgaddr(display.getImgaddr());
			tbDisplay.setUserhomeworkid(display.getUserhomeworkid());
			tbDisplay.setUserid(display.getUserid());
			flag = tbDisplyMapper.updateByPrimaryKey(tbDisplay);
		}
		if(flag==1){
			return true;
		}else{
			return false;
		}
	}
}
